package com.ssm.common;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.ssm.common.bean.Result;

public abstract class BaseController {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	protected String[] cloums = null;

	/**
	 * ajax失败
	 * 
	 * @param msg
	 *            失败的消息
	 * @return {Object}
	 */
	public Result renderError(String msg) {
		Result result = new Result();
		result.setMsg(msg);
		return result;
	}

	/**
	 * ajax成功
	 * 
	 * @return {Object}
	 */
	public Result renderSuccess() {
		Result result = new Result();
		result.setSuccess(true);
		return result;
	}

	/**
	 * ajax成功
	 * 
	 * @param obj
	 *            成功时的对象
	 * @return {Object}
	 */
	public Result renderSuccess(String msg,Object obj) {
		Result result = new Result();
		result.setSuccess(true);
		result.setMsg(msg);
		result.setObj(obj);
		return result;
	}

	public void writeResponse(HttpServletResponse response, String data) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		try {
			response.getWriter().print(data);
		} catch (Exception e) {
			JSONObject json = new JSONObject();
			json.put("info", "error");
			try {
				response.getWriter().print(json.toString());
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
}
